function wuyuan5(R0, x, w = [0.25, 0.25, 0.25, 0.25]) {
    let n = R0.length;//4

    let mm = R0[0].length;//8

    let m = mm / 2;//4
    let a = [], b = [];
    for (let i = 0; i < n; i++) {
        a[i] = []
        b[i] = [];
        for (let j = 0; j < m; j++) {
            a[i][j] = R0[i][j * 2];
            b[i][j] = R0[i][j * 2 + 1];
        }
    }
    let ap = [], bp = [];
    for (let i = 0; i < n; i++) {
        ap[i] = R0[i][0];
        bp[i] = R0[i][mm - 1];
    }
    let fah = x.length;//1
    let fal = x[0].length;//4
    let k = [];
    for (let i = 0; i < n; i++) {
        k[i] = [];
        for (let j = 0; j < m; j++) {
            k[i][j] = 0;
        }
    }
    let index = [];
    let jstar = 0;
    for (let g = 0; g < fal; g++) {
        for (let i = 0; i < fah; i++) {
            for (let j = 0; j < m; j++) {
                if (Math.abs(b[i][j] - a[i][j]) < Number.EPSILON) {
                    k[i][j] = 0;
                } else if (a[i][j] == b[i][j]) {
                    if (x[i][g] == a[i][j]) {
                        k[i][j] = Number.MIN_VALUE;
                    } else {
                        let pp = Math.abs(x[i][g] - 0.5 * (ap[i] + bp[i])) - 0.5 * (bp[i] - ap[i]);
                        k[i][j] = Math.abs(x[i][g] - a[i][j]) / (pp - Math.abs(x[i][g] - a[i][j]));
                    }
                } else {

                    if (x[i][g] >= a[i][j] && x[i][g] <= b[i][j]) {
                        let p = Math.abs(x[i][g] - 0.5 * (a[i][j] + b[i][j])) - 0.5 * (b[i][j] - a[i][j]);
                        k[i][j] = -p / Math.abs(b[i][j] - a[i][j]);
                    } else {
                        let p = Math.abs(x[i][g] - 0.5 * (a[i][j] + b[i][j])) - 0.5 * (b[i][j] - a[i][j]);
                        let pp = Math.abs(x[i][g] - 0.5 * (ap[i] + bp[i])) - 0.5 * (bp[i] - ap[i]);
                        k[i][j] = p / (pp - p);
                    }
                }
            }
        }
        let kp = [];
        for (let j = 0; j < m; j++) {
            let sum = 0;
            for (let i = 0; i < w.length; i++) {
                sum += w[i] * k[i][j];
            }
            kp[j] = sum;
        }
        let kpmaxIndex = 0;
        let kpmax = kp[0];
        let kpmin = kp[0];
        for (let i = 1; i < kp.length; i++) {
            if (kp[i] > kpmax) {
                kpmax = kp[i];
                kpmaxIndex = i;
            }
            if (kp[i] < kpmin) {
                kpmin = kp[i];
            }
        }
        index.push(kpmaxIndex);
        let avkpp = [];
        let avkppSum = 0;
        for (let i = 0; i < m; i++) {
            avkpp[i] = (kp[i] - kpmin) / (kpmax - kpmin);

            avkppSum += avkpp[i];
        }

        let jup = 0;
        for (let i = 0; i < m; i++) {
            jup += i * avkpp[i];

        }

        jstar = jup / avkppSum;
        // console.log(jup, avkppSum, '&', k)

    }

    function getLevel(arr) {
        let result = []
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] == 0) {
                result[i] = '差'
            } else if (arr[i] == 1) {
                result[i] = '中'
            } else if (arr[i] == 2) {
                result[i] = '良'
            } else {
                result[i] = '优'
            }
        }
        return result
    }

    // console.log(jstar)
    jstar = Math.round(jstar)
    // 根据评价结果，判断属于哪一级别
    // if (jstar == 0) {
    //     return "差";
    // } else if (jstar == 1) {
    //     return "中";
    // } else if (jstar == 2) {
    //     return "良";
    // } else if (jstar == 3) {
    //     return "优";
    // } else {
    //     return '未知'
    // }
    return getLevel(index)
}

// const R0 = [[0, 10, 11, 20, 21, 30, 31, 40],
//     [0, 10, 11, 20, 21, 30, 31, 40],
//     [0, 10, 11, 20, 21, 30, 31, 40],
//     [0, 10, 11, 20, 21, 30, 31, 40]]
// const x = [[21, 27, 33, 24],
//     [22, 22, 22, 13],
//     [33, 33, 11, 11],
//     [35, 33, 38, 35]]
//
// console.log(wuyuan5(R0, x))
module.exports = wuyuan5
